﻿using KYJDAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using thinger.DataConvertLib;

namespace KYJBLL
{
    public class SQLiteManage
    {
        private SQLiteService sQLiteService = new SQLiteService();
        public void SetConnStr(string connString)
        {
            sQLiteService.SetConnStr(connString);
        }

        /// <summary>
        /// 判断数据表是否存在
        /// </summary>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public bool IsTableExits(string tableName)
        {
            return sQLiteService.IsTableExits(tableName);
        }

        /// <summary>
        /// 删除数据表
        /// </summary>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public bool DeleteTable(string tableName)
        {
            return sQLiteService.DeleteTable(tableName);
        }

        /// <summary>
        /// 清除表数据内存
        /// </summary>
        /// <returns></returns>
        public bool VacuumTable()
        {
            return sQLiteService.VacuumTable();
        }
        /// <summary>
        /// 创建数据表
        /// </summary>
        /// <param name="tableNaem"></param>
        /// <param name="columns"></param>
        /// <param name="isPrimaryKey"></param>
        /// <returns></returns>
        public bool CreateTable(string tableNaem, List<string> columns, bool isPrimaryKey = true)
        {
            return sQLiteService.CreateTable(tableNaem, columns, isPrimaryKey);
        }

        /// <summary>
        ///重新创建数据表
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="columns"></param>
        /// <returns></returns>
        public OperateResult ReCreateActualTable(string tableName, List<string> columns)
        {
            if (IsTableExits(tableName))
            {
                if (DeleteTable(tableName) == false)
                {
                    return OperateResult.CreateFailResult("删除数据表失败");
                }
                return OperateResult.CreateSuccessResult();
            }
            if (CreateTable(tableName, columns) == false)
            {
                return OperateResult.CreateFailResult("创建数据表失败");
            }
            VacuumTable();
            return OperateResult.CreateSuccessResult();
        }
    }
}
